package com.lframework.xingyun.basedata.mappers;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.github.yulichang.toolkit.JoinWrappers;
import com.lframework.starter.web.core.mapper.BaseMapper;
import com.lframework.xingyun.basedata.bo.member.QueryMemberLevelDetailBo;
import com.lframework.xingyun.basedata.entity.Member;
import com.lframework.xingyun.basedata.entity.MemberLevel;
import com.lframework.xingyun.basedata.entity.MemberLevelDetail;
import com.lframework.xingyun.basedata.vo.member.QueryMemberLevelDetailVo;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 会员等级生效明细 Mapper 接口
 * </p>
 *
 * @author mos
 */
public interface MemberLevelDetailMapper extends BaseMapper<MemberLevelDetail> {

    /**
     * 查询列表
     * @param vo
     * @return
     */
    default IPage<QueryMemberLevelDetailBo> query(QueryMemberLevelDetailVo vo) {
        return defaultJoinWrapper()
                .leftJoin(Member.class, Member::getId, MemberLevelDetail::getMemberId)
                .selectAssociation(Member.class, QueryMemberLevelDetailBo::getMember)
                .leftJoin(MemberLevel.class, MemberLevel::getId, MemberLevelDetail::getLevelId)
                .selectAssociation(MemberLevel.class, QueryMemberLevelDetailBo::getLevel)
                .eq(MemberLevelDetail::getMemberId, vo.getMemberId())
                .eq(StrUtil.isNotBlank(vo.getLevelId()), MemberLevelDetail::getLevelId, vo.getLevelId())
                .eq(Objects.nonNull(vo.getActivate()), MemberLevelDetail::getActivate, vo.getActivate())
                .orderByDesc(MemberLevelDetail::getCreateTime)
                .page(PageDTO.of(vo.getPageIndex(), vo.getPageSize()), QueryMemberLevelDetailBo.class);
    }

    /**
     * 查询最后生效的会员等级明细
     * @param memberId 会员ID
     * @return 会员等级生效明细
     */
    default QueryMemberLevelDetailBo queryLastActiveDetailByMemberId(String memberId) {
        return defaultJoinWrapper()
                .leftJoin(Member.class, Member::getId, MemberLevelDetail::getMemberId)
                .selectAssociation(Member.class, QueryMemberLevelDetailBo::getMember)
                .leftJoin(MemberLevel.class, MemberLevel::getId, MemberLevelDetail::getLevelId)
                .selectAssociation(MemberLevel.class, QueryMemberLevelDetailBo::getLevel)
                .eq(MemberLevelDetail::getMemberId, memberId)
                .gt(MemberLevel::getEffectiveDay, 0)
                .orderByDesc(MemberLevelDetail::getBeginDate)
                .first(QueryMemberLevelDetailBo.class);
    }

    /**
     * 查询可以生效的会员等级明细
     * @param memberId 会员ID
     * @param date 时间
     * @return 会员等级生效明细
     */
    default MemberLevelDetail queryCanActiveDetailByMemberId(String memberId, LocalDateTime date) {
        return defaultJoinWrapper()
                .leftJoin(Member.class, Member::getId, MemberLevelDetail::getMemberId)
                .leftJoin(MemberLevel.class, MemberLevel::getId, MemberLevelDetail::getLevelId)
                .eq(MemberLevelDetail::getMemberId, memberId)
                .le(MemberLevelDetail::getBeginDate, date)
                .gt(MemberLevelDetail::getEndDate, date)
                .orderByDesc(MemberLevel::getLevel)
                .orderByAsc(MemberLevelDetail::getBeginDate)
                .first(MemberLevelDetail.class);
    }

    /**
     * 失效当前生效的会员等级明细
     * @param memberId 会员ID
     * @return 生效会员等级明细
     */
    default int invalidateActiveDetail(String memberId) {
        return JoinWrappers.update(MemberLevelDetail.class)
                .set(MemberLevelDetail::getActivate, false)
                .eq(MemberLevelDetail::getMemberId, memberId)
                .update();
    }

    /**
     * 生效会员等级明细
     * @param detailId 生效会员等级明细ID
     * @return 生效会员等级明细
     */
    default int activateDetail(String detailId) {
        return JoinWrappers.update(MemberLevelDetail.class)
                .set(MemberLevelDetail::getActivate, true)
                .eq(MemberLevelDetail::getId, detailId)
                .update();
    }

}

